#!/bin/bash
# MTM event manager
# Chris Dorman (C) 2021 - CC-BY-SA 3.0

wrkdir=`pwd`
events="mtm_db/events"
pids="mtm_db/pids"

while true
do	
	cd $wrkdir/$events

	for x in *
	do
		if [ -f "$x" ]; then
			execcmd=`cat $x`
			rm $x
			cd $wrkdir
			`$execcmd` &

			cd $events
		fi
	done
	
	cd $wrkdir/$pids
	
	for y in *
	do
		if [ -f "$y" ]; then
				if ps -p $(cat $y) > /dev/null 
				then
					cd $wrkdir/mtm_db/users
					echo "<div style='display:inline;color:#00ff00;'>Running</div>" > $y.status
					echo -n "true" > $y.statuscode
				else 
					cd $wrkdir/mtm_db/users
					echo "<div style='display:inline;color:#ff0000;'>Stopped</div>" > $y.status
					echo -n "false" > $y.statuscode
				fi
			cd $wrkdir/$pids
		fi
	done
	sleep 1
done
